import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/knowledgeword.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/widgets/buttons/debouncedMaterialButton.dart';
import 'package:toggle_switch/toggle_switch.dart';
import 'package:ichinesereader/widgets/cards/folder.dart';
import 'package:ichinesereader/models/knowledgeword/folderKnowledgeWordsDto.dart';

import '../../themes/appTheme.dart';

// ignore: must_be_immutable
class SelectFolderDialog extends GetView<KnowledgeWordsController> {
  SelectFolderDialog(
      {Key? key,
      required this.title,
      required this.fromFolderId,
      required this.from})
      : super(key: key);

  String title;
  String fromFolderId;
  String from;
  bool keepFlag = false;

  void moveToFolder() async {
    FolderKnowledgeWordsDto dto;
    if (controller.mainMode.value)
      dto = controller.getSelectedFolderAWord();
    else
      dto = controller.getSelectedRecycledFolderAWord();

    String? folderId = controller.folderdir.length > 0
        ? controller.folderdir[controller.folderdir.length - 1]
        : '';
    if (((dto.folderIds?.length == 0) && (dto.knowledgeIds?.length == 0)) ||
        (folderId == '')) {
      showSnackMsg('Please select folder or wordcard to move');
      // Get.snackbar('Message'.tr, 'Please select folder or wordcard to move'.tr,
      //     backgroundColor: Get.theme.primaryColorDark,
      //     colorText: Get.theme.primaryColorLight,
      //     snackPosition: SnackPosition.BOTTOM);
      return;
    }
    if (fromFolderId == folderId) {
      showSnackMsg('Target folder is same with current folder');
      // Get.snackbar('Message'.tr, 'Target folder is same with current folder'.tr,
      //     backgroundColor: Get.theme.primaryColorDark,
      //     colorText: Get.theme.primaryColorLight,
      //     snackPosition: SnackPosition.BOTTOM);
      return;
    }

    if (from == 'myWords') {
      controller.moveKnowledgeWords(
          dto, fromFolderId, folderId.toString(), keepFlag);
    } else if (from == 'basic100') {
      controller.saveKnowledgeWords(dto, folderId.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Flexible(
              child: Padding(
                padding: const EdgeInsets.only(right: 21.0),
                child: Text(
                  this.title,
                  textAlign: TextAlign.right,
                  style: TextStyle(
                      fontSize: 30.0,
                      decoration: TextDecoration.underline,
                      decorationStyle: TextDecorationStyle.dotted),
                ),
              ),
            ),
            InkWell(
              onTap: () {
                Navigator.of(context).pop();
                controller.changeFolderDir(0);
                controller.getFolders();
              },
              child: Icon(Icons.cancel,
                  color: Get.isDarkMode ? Colors.white : Colors.blueGrey),
            ),
          ],
        ),
        titleTextStyle: TextStyle(
            letterSpacing: 1,
            color: Get.isDarkMode ? Colors.white : Colors.blueGrey,
            fontSize: 20),
        backgroundColor:
            Get.isDarkMode ? AppTheme.colors.darkTheme2 : Colors.white,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(
            Radius.circular(6.0),
          ),
        ),
        content: Container(
          height: 450,
          width: 900,
          child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: <Widget>[
                    Expanded(child: Obx(() {
                      List<Widget> children = [];

                      for (int i = 0;
                          i < controller.folderDirName.length;
                          i++) {
                        String? name = controller.folderDirName[i];
                        children.add(InkWell(
                          child: Text(
                            "$name/",
                            style: TextStyle(
                                color: Get.isDarkMode
                                    ? Get.theme.primaryColorLight
                                    : Colors.grey),
                          ),
                          onTap: () {
                            controller.changeFolderDir(i);
                          },
                        ));
                      }
                      return Row(
                        // child: Text(text),
                        children: children,
                      );
                    })),
                    if (from == 'myWords')
                      Padding(
                        padding: const EdgeInsets.all(12.0),
                        child: ToggleSwitch(
                          minWidth: 90.0,
                          initialLabelIndex: 0,
                          cornerRadius: 3.0,
                          activeFgColor:
                              Get.isDarkMode ? Colors.black : Colors.white,
                          inactiveBgColor: Get.isDarkMode
                              ? Get.theme.primaryColor
                              : Colors.grey,
                          inactiveFgColor:
                              Get.isDarkMode ? Colors.black : Colors.white,
                          totalSwitches: 2,
                          labels: ['Move', 'Copy'],
                          activeBgColors: Get.isDarkMode
                              ? [
                                  [Colors.white],
                                  [Colors.white]
                                ]
                              : [
                                  [AppTheme.colors.orangeBtn],
                                  [AppTheme.colors.orangeBtn]
                                ],
                          onToggle: (index) {
                            this.keepFlag = index == 0 ? false : true;
                          },
                        ),
                      )
                  ],
                ),
                Expanded(
                  child: controller.obx(
                    (data) {
                      return Padding(
                        padding: const EdgeInsets.only(top: 21.0),
                        child: GridView.builder(
                            gridDelegate:
                                const SliverGridDelegateWithMaxCrossAxisExtent(
                                    maxCrossAxisExtent: 150,
                                    childAspectRatio: 116 / 150,
                                    crossAxisSpacing: 10,
                                    mainAxisSpacing: 10),
                            itemCount: controller.folders.length,
                            itemBuilder: (context, index) {
                              return FolderCard(controller.folders[index]);
                            }),
                      );
                    },
                    onEmpty: Center(child: Text('No cards or folders'.tr)),
                    onLoading: Center(child: CircularProgressIndicator()),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 21.0),
                  child: Center(
                    child: Container(
                      width: 210.0,
                      height: 45.0,
                      child: DebouncedMaterialButton(
                        color: Get.isDarkMode
                            ? (Colors.white)
                            : Theme.of(context).primaryColor,
                        textColor: Get.isDarkMode
                            ? (Colors.black)
                            : Theme.of(context).primaryColorLight,
                        onPressed: () {
                          this.moveToFolder();
                          Navigator.of(context).pop();
                          controller.changeFolderDir(0);
                          controller.getFolders();
                        },
                        child: Text(
                          'Submit'.tr,
                          style: TextStyle(fontSize: 18.0),
                        ),
                      ),
                    ),
                  ),
                ),
              ]),
        ));
  }
}
